import mx.utils.Delegate;

import gugga.components.XMLLoader;
import gugga.events.EventDispatcher;
import gugga.utils.DebugUtils;
import gugga.utils.XML2Object;

/**
 * @author bony
 * The class "ApplicationStructureDataManager", loads the application datat, 
 * and convert it from XML2Object format to format necessary 
 * for the LayoutGenerator. 
 */
class externalContent.ApplicationStructureDataManager extends EventDispatcher 
{ 	
	// List the constants. 
	private var SUBITEMS_DIRECTION: String = "right";
	private var SUBITEMS_OFFSET: Number = 10; 
	private var XML_PATH: String = "application.xml";
	
	private var mApplicationXML: XMLLoader;

	public function loadData(): Void
	{
		// Create XMLLoader to get the "application.xml".
		mApplicationXML = new XMLLoader();		
		
		// "mApplicationXML" loads the "application.xml".
		mApplicationXML.load(XML_PATH);	
		
		mApplicationXML.addEventListener("completed", Delegate.create(this, onApplicationXMLCompleted));
	}
	
	private function onApplicationXMLCompleted() : Void 
	{
		dispatchEvent({type:"dataLoaded", target:this});
	}
	
	public function getNavigationData() : Object 
	{		
		var applicationXML2Object : Object;
		
		// Parse the application.xml to XML2Object format.
		applicationXML2Object = XML2Object.parse(mApplicationXML.Xml);		
		
		// Get the LayoutGenerator data for the navigation.
		var navigationData: Object = generateNavigationData(applicationXML2Object);		
		
		return navigationData;
	}

	// This function transform the data from xml(application.xml) to XML2Object format. This is the 
	// format used by the LayoutGenerator.
	// Some of the attributes(sub_items_direction, sub_items_offset) are equal for all items, therefore 
	// they aren't in the xml but constants in this class.   
	private function generateNavigationData(aData : Object) : Object
	{
		// This is the skeleton of the LayoutGenerator data.
		// Use short implementation to create the XML2Object format.  
		var navigationData: Object = 
		{
			items :
			{
				attributes :
				{
					sub_items_direction : SUBITEMS_DIRECTION,
					sub_items_offset : SUBITEMS_OFFSET
				},
						
				item : []
			}	
		
		};
		
		// Create array that will contain the data for all sections.
		var items : Array = navigationData.items.item;
		var tempItem : Object;
		
		// Get the data from the xml in array.
		var sections : Array = XML2Object.getAsArray(aData["sections"]["section"]);
		
		// Add all items(sections) in arrey items.
		for(var i:Number = 0; i < sections.length; i++)
		{
			tempItem = generatePrimaryNavigationSingleItemData(sections[i]);
			items.push(tempItem);	
		} 
		
		return navigationData;
	}

	// This function returns object which contains the data for the section.
	// ID - the identifier of the section
	// Label - the text of the button which links to the section
	// instance_name - the instance name of the section 
	// symbol_identifier - the linkage of the button in the flash file library.
	private function generatePrimaryNavigationSingleItemData(aData:Object) : Object
	{			
		var itemID : String = aData.attributes["ID"];
		
		var itemLabel : String = aData.attributes["label"];	
		
		var result : Object;

		result = 
			{
				attributes : 
				{
					ID : itemID,
					Label : itemLabel,	
					instance_name : itemID, 
					symbol_identifier : "LabelNavigationButton"							
				}
			};		
		
		return result;
	}
	
		
	// This function gets sections data as array in XML2Object format. 
	public function getSectionData() :Array
	{
		var result: Array;
		
		result = XML2Object.getAsArray(XML2Object.parse(mApplicationXML.Xml)["sections"]["section"]); 
		
		return result; 
	}
	
}